/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package postersender;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Berk
 */
public class DBHandler {

    private Connection connection;
    private Object lock;

    public DBHandler() {
        try {
            String url = Constants.Url;
            String login = Constants.login;
            String password = Constants.password;
            String driver = Constants.driver;
            Class.forName(driver);
            this.connection = DriverManager.getConnection(url, login, password);

        } catch (SQLException ex) {
            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    public void insertFlag(Flag newFlag) {
        try {
            synchronized(lock){
                String query = "insert into flag_table values(?,?,?)";
                PreparedStatement statement = this.connection.prepareStatement(query);
                statement.setString(1, newFlag.getFlag());
                statement.setString(2, newFlag.getTime());
                statement.setString(3, newFlag.getStatus());
                statement.executeUpdate();
                statement.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized void changeStatus(String newFlag, String newStatus) {
        try {
            synchronized (lock) {
                String query = "update flag_table set status = ? where flag = ?";
                PreparedStatement statement = this.connection.prepareStatement(query);
                statement.setString(1, newStatus);
                statement.setString(2, newFlag);
                statement.executeUpdate();
                statement.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public synchronized ResultSet getUnsentFlags() {
        ResultSet rs = null;
        try {
            synchronized (lock) {
                String query = "Select flag From flag_table Where status = Waiting";
                PreparedStatement ps = this.connection.prepareStatement(query);
                rs = ps.executeQuery();
            }
        } catch (SQLException ex) {

            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    public synchronized ResultSet getAllFlags() {
        ResultSet rs = null;
        try {
            synchronized (lock) {
                String query = "Select * From flag_table";
                PreparedStatement ps = this.connection.prepareStatement(query);
                rs = ps.executeQuery();
            }
        } catch (SQLException ex) {

            Logger.getLogger(DBHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
}
